// let BASE_URL = "http://fx.nanjingyunpeng.cn/";
let BASE_URL = "https://fuxin918.net/";


const request = (obj, data, loading) => {
	if (loading) uni.showLoading({
		title: "加载中",
		mask: true
	});
	let Authorization = window.sessionStorage.getItem('token') || ""
	let httpDefaultOpts = {
		url: BASE_URL + obj.url,
		data: data,
		beforeSend: function(xmlHttp) {
			xmlHttp.setRequestHeader("If-Modified-Since", "0");
			xmlHttp.setRequestHeader("Cache-Control", "no-cache");
		},
		method: obj.method || 'post',
		dataType: "json",
		header: {
			"Content-type": "application/json",
		},
	};
	if (Authorization) {
		if (httpDefaultOpts.header) {
			// console.log("1111")
			httpDefaultOpts.header = {
				Authorization: "Bearer " + ' ' + Authorization
			};
		} else {
			// console.log("2222")
			httpDefaultOpts.header["Authorization"] = Authorization;
		}
	} else {
		// console.log("获取不到userToken");
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts)
			.then((res) => {
				// console.log("接口数据", res)
				if (loading) uni.hideLoading();
				if (res.statusCode == 200) {
					if (res.data.code == 1000) {
						resolve(res.data);
					} else if (res.data.code == 1001) {
						uni.showToast({
							title: '无此用户！',
							icon: "none",
							duration: 1500
						});
						uni.removeStorageSync("token")
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/login/index"
							});
						}, 2000)
						return
					} else if (res.data.code == 1004) {
						// console.log("1004444444444")
						uni.showToast({
							title: '请登录后查看！',
							icon: "none",
							duration: 1500
						});
						// uni.removeStorageSync("token")
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/login/index"
							});
						}, 1800)
						return
					} else if (res.data.code == 1002) {
						uni.showToast({
							title: '验证已过期，请重新登录！',
							icon: "none",
							duration: 1500
						});
						// uni.removeStorageSync("token")
						window.sessionStorage.removeItem('token')
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/login/index"
							});
						}, 1800)
						return
					} else if (res.data.code == 1003) {
						uni.showToast({
							title: '验证已过期，请重新登录！',
							icon: "none",
							duration: 1500
						});
						// uni.removeStorageSync("token")
						window.sessionStorage.removeItem('token')
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/login/index"
							});
						}, 1800)
						return
					} else if (res.data.code == 1003) {
						uni.showToast({
							title: '验证失败，请重新登录！',
							icon: "none",
							duration: 1500
						});
						window.sessionStorage.removeItem('token')
						// uni.removeStorageSync("token")
						setTimeout(() => {
							uni.reLaunch({
								url: "/pages/login/index"
							}, 1800);
						})
						return
					} else if (res.data.code == 2000) {
						resolve(res.data);
						return
					} else {
						reject(res);
					}
				}


			})
			.catch((response) => {
				reject(response);
			});
	});
	return promise;
};
export default {
	BASE_URL,
	request,
};